package models

import (
	"gitchina/kly_service/data"
	"gitchina/kly_service/errors"
	"gitchina/kly_service/util"
	"strconv"
)

type AddRoleRQ struct {
	MbrType int `json:"member_type"`
}

type UpdateIDAuthStateRQ struct {
	Name     string `json:"name"`
	IDCardNo string `json:"id_card_no"`
}

type AuthStateRS struct {
	MbrType     int    `json:"member_type"`
	PAuthState  int    `json:"p_auth_state"`
	EAuthState  int    `json:"e_auth_state"`
	StateReason string `json:"state_reason"`
}

type IDCardRS struct {
	IDCardFS string `json:"id_card_fs"`
	IDCardRS string `json:"id_card_rs"`
}

func (m Member) GetAuthState(mbrId ID) (rs AuthStateRS, err error) {
	var id int64
	var repo data.MemberRepo
	var mbrType, pAuthState, eAuthState, stateReason string
	_, _, _, id, err = mbrId.Decode()
	if err != nil {
		util.GetLogger().Error("[model - GetAuthState] - error. unexpected mbrId:%s", string(mbrId))
		err = errors.New(errors.MSG_Mbr_Invalid)
	} else {
		mbrType, pAuthState, eAuthState, stateReason, err = repo.GetAuthState(int(id))
		if err != nil {
			util.GetLogger().Error("[model - GetAuthState] - error:%s", err.Error())
			err = errors.New(errors.MSG_Mbr_Get_Auth_State_Error)
		} else {
			rs.MbrType, err = strconv.Atoi(mbrType)
			rs.PAuthState, err = strconv.Atoi(pAuthState)
			rs.EAuthState, err = strconv.Atoi(eAuthState)
			rs.StateReason = stateReason
		}
	}
	return
}

func (m Member) UpdateIDAuthState(mbrId ID, name, idCardNo string) (err error) {
	var id int64
	var authState string
	var repo data.MemberRepo
	_, _, _, id, err = mbrId.Decode()
	if err != nil {
		util.GetLogger().Error("[model-UpdateIDAuthState] - error. unexpected mbrId:%s", string(mbrId))
		err = errors.New(errors.MSG_Mbr_Invalid)
	} else {
		if _, authState, _, _, err = repo.GetAuthState(int(id)); err == nil {
			if authState != "2" {
				if err = repo.UpdateUserNameAndIDCardNo(int(id), name, idCardNo); err == nil {
					repo.UpdateAuthState(int(id), 1)
				} else {
					util.GetLogger().Error("[model-UpdateIDAuthState] - error:%s", err.Error())
					err = errors.New(errors.MSG_Mbr_Upd_Name_Or_IDCardNo_Error)
				}
			}
		}
		//if len(idCardNo) != 18 && len(idCardNo) != 15 {
		//	err = errors.New(errors.MSG_Mbr_Invalid_ID_Card_No)
		//} else {
		//
		//}
	}
	return
}

func (m Member) AddRole(mbrId ID, mbrType int) (err error) {
	var id int64
	var repo data.MemberRepo
	if err = checkMemberType(mbrType); err == nil {
		_, _, _, id, err = mbrId.Decode()
		if err != nil {
			util.GetLogger().Error("[model-AddRole] - error. unexpected mbrId:%s", string(mbrId))
			err = errors.New(errors.MSG_Mbr_Invalid)
		} else {
			if err = repo.AddRole(int(id), strconv.Itoa(mbrType)); err != nil {
				util.GetLogger().Error("[model-AddRole] - error:%s", err.Error())
				err = nil // maybe primary key conflict ignore.
			}
		}
	}
	return err
}

func (m Member) GetPersonalAuthInfo(mbrId ID) (rs IDCardRS, err error) {
	var id int64
	_, _, _, id, err = mbrId.Decode()
	if err != nil {
		util.GetLogger().Error("[model - GetPersonalAuthInfo] - error. unexpected mbrId:%s", string(mbrId))
		err = errors.New(errors.MSG_Mbr_Invalid)
	} else {
		var repo data.MemberRepo
		rs.IDCardFS, rs.IDCardRS, err = repo.GetPersonalAuthInfo(int(id))
	}
	return
}
